package com.xyz.admin.common.aspect;

import com.sc.base.common.exception.RRException;
import groovy.util.logging.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
@Order(10)
public class LogAspect {

    private Logger logger = LoggerFactory.getLogger(IdempotentAspect.class);

    //配置切入点，即执行的位置
    @Pointcut("execution(* com.xyz.admin.modules.*.controller.*.*(..))")
    public void pointCut(){
    }

    //使用环绕通知，在方法的执行前后分别记录一个时间相减
    @Around("pointCut()")
    public Object doLog(ProceedingJoinPoint proceedingJoinPoint){
        long startTime = System.currentTimeMillis();
        Object object=null;
        try {
            object=proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }finally {
            long endTime = System.currentTimeMillis();
            long time = endTime-startTime;
            if(time>200){
                throw  new RRException("方法执行时间过长，耗时:"+time+"ms，请修改",1111);
            }
            logger.info("{}方法执行了：{}ms",proceedingJoinPoint.getSignature().toShortString(),time);
        }
        return object;
    }
}
